/*
 *  Copyright 2010 Mahbub Mozadded Sowrov.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package com.sowrov.gui.model.tablemodel;

import com.sowrov.util.logsystem.ChronicleLogger;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

/**
 * 
 * @author Sowrov
 */
public class CustomTableModel extends AbstractTableModel {
    //private Object data[][];
    private ArrayList<ArrayList<CustomTableObject>> data;
    private String[] column;

    //boolean editable[][];
    public CustomTableModel(final String[] columnName) {
        column = columnName;
        data = new ArrayList<ArrayList<CustomTableObject>>();
    } // end CustomTableModel()

    public final void setCellEditable(final int row, final int col, final boolean flag) {
        this.data.get(row).get(col).setEditable(flag);
    } // end setCellEditable()

    @Override
    public final boolean isCellEditable(final int rowIndex, final int columnIndex) {
        return this.data.get(rowIndex).get(columnIndex).isEditable();
    } // end isCellEditable()

    @Override
    public final Class getColumnClass(final int columnIndex) {
        if (this.data.isEmpty()) {
            ChronicleLogger.getInstance().logDebug("CustomTableModel->getColumnClass column Index " + columnIndex + " Out of Bound");

            return null;
        } // end if

        if (columnIndex > this.data.get(0).size()) {
            ChronicleLogger.getInstance().logDebug("CustomTableModel->getColumnClass column Index " + columnIndex + " Out of Bound");

            return null;
        } // end if

        return this.data.get(0).get(columnIndex).getClass();
    } // end getColumnClass()

    public final int getColumnCount() {
        return column.length;
    } // end getColumnCount()

    public final void setColumnEditable(final int columnIndex, final boolean flag) {
        for (int i = 0; i < getRowCount(); ++i) {
            this.data.get(i).get(columnIndex).setEditable(flag);
        } // end for
    } // end setColumnEditable()

    @Override
    public final String getColumnName(final int columnIndex) {
        return column[columnIndex];
    } // end getColumnName()

    public final int getRowCount() {
        return data.size();
    } // end getRowCount()

    public final void setRowEitible(final int rowIndex, final boolean flag) {
        for (int i = 0; i < this.getColumnCount(); i++) {
            this.setCellEditable(rowIndex, i, flag);
        } // end for
    } // end setRowEitible()

    @Override
    public final void setValueAt(final Object value, final int rowIndex, final int columnIndex) {
        //data[rowIndex][columnIndex] = value;
        if (rowIndex >= this.data.size()) {
            ChronicleLogger.getInstance().logDebug("CustomTableModel->setValueAt row Index " + rowIndex + " Out of Bound");

            return;
        } // end if

        if (columnIndex > this.data.get(rowIndex).size()) {
            ChronicleLogger.getInstance().logDebug("CustomTableModel->setValueAt column Index Out of Bound");

            return;
        } // end if

        CustomTableObject obj = this.data.get(rowIndex).get(columnIndex);
        obj.setObj(value);
        this.data.get(rowIndex).set(columnIndex, obj);
    } // end setValueAt()

    public final Object getValueAt(final int rowIndex, final int columnIndex) {
        return data.get(rowIndex).get(columnIndex).getObj();
    } // end getValueAt()

    @Override
    public final int findColumn(final String columnName) {
        for (int i = 0; i < getColumnCount(); i++) {
            if (columnName.equals(getColumnName(i))) {
                return i;
            } // end if
        } // end for

        return -1;
    } // end findColumn()

    public final void insertRow(final Object[] obj) {
        ArrayList<CustomTableObject> tableObject = new ArrayList<CustomTableObject>();

        for (int i = 0; i < obj.length; i++) {
            tableObject.add(new CustomTableObject(obj[i], true));
        } // end for

        this.data.add(tableObject);
        this.fireTableDataChanged();
    } // end insertRow()

    public final void removeRow(final int i) {
        if ((i < 0) || (i > this.data.size())) {
            return;
        } // end if

        this.data.remove(i);
        this.fireTableDataChanged();
    } // end removeRow()

    public void removeAllRow(){
        if (!this.data.isEmpty()) {
            this.data.clear();
            this.fireTableDataChanged();
        }
    }
} // end CustomTableModel
